class Promise {
  //构造方法
  constructor(executor) {
    //为实例对象添加属性
    this.PromiseState = 'pending';
    this.PromiseResult = undefined;
    this.callbacks = [];
    //success 
    let success = value => {
      //判断
      if (this.PromiseState !== 'pending') return;
      //改变状态
      this.PromiseState = 'fulfilled';
      //改变结果值
      this.PromiseResult = value;
      //执行成功的回调
      this.callbacks.forEach(obj => {
        setTimeout(() => {
          //执行成功的回调
          obj.ok();
        });
      })
    }

    //fail
    let fail = reason => {
      //判断状态是否为 pending
      if (this.PromiseState !== 'pending') return;
      //改变状态
      this.PromiseState = 'rejected';
      //改变结果值
      this.PromiseResult = reason;
      //执行失败的回调
      this.callbacks.forEach(obj => {
        setTimeout(() => {
          //执行失败的回调
          obj.ng();
        })
      })
    }

    try {
      //执行 执行器函数
      executor(success, fail);
    } catch (e) {
      //改变 promise 实例对象的状态为失败
      fail(e);
    }
  }

  //为实例对象添加 then 方法
  then(onFulfilled, onRejected) {
    //判断 onRejected
    if (typeof onRejected !== 'function') {
      //给 onRejected 赋一个默认值
      onRejected = reason => {
        throw reason;
      }
    }
    //判断 onFulfilled
    if (typeof onFulfilled !== 'function') {
      onFulfilled = value => value;
    }

    return new Promise((resolve, reject) => {
      //封装函数
      let callback = (type) => {
        try {
          //调用回调函数, 获得返回值
          let res = type(this.PromiseResult);
          if (res instanceof Promise) {
            //如果是一个 promise 实例对象 根据 promise 决定 then 的返回值
            res.then(v => {
              resolve(v);
            }, r => {
              reject(r);
            });
          } else {
            // 如果不是 promise   成功
            resolve(res);
          }
        } catch (e) {
          reject(e);
        }
      }
      //判断实例对象的状态是否为成功
      if (this.PromiseState === 'fulfilled') {
        setTimeout(() => {
          callback(onFulfilled);
        });
      }
      //判断实例对象的状态是否为失败
      if (this.PromiseState === 'rejected') {
        setTimeout(() => {
          callback(onRejected);
        });

      }
      //判断 pending 状态
      if (this.PromiseState === 'pending') {
        //将回调函数保存在实例对象身上
        this.callbacks.push({
          ok: function () {
            callback(onFulfilled);
          },
          ng: function () {
            callback(onRejected);
          }
        });
      }
    })
  }

  //catch 方法
  catch (onRejected) {
    return this.then(undefined, onRejected);
  }

  //resolve
  static resolve(value) {
    //返回一个promise实例对象
    return new Promise((resolve, reject) => {
      if (value instanceof Promise) {
        value.then(resolve, reject);
      } else {
        resolve(value);
      }
    })
  }

  //reject
  static reject(reason) {
    //返回一个promise实例对象
    return new Promise((resolve, reject) => {
      reject(reason);
    })
  }

  //all
  static all(promises) {
    return new Promise((resolve, reject) => {
      //声明一个数组
      let arr = [];
      let counter = 0;
      //遍历数组
      promises.forEach((item, index) => {
        //调用 then 方法
        item.then(v => {
          //如果成功 
          arr[index] = v;
          //判断是否全部成功了
          counter++;
          //判断
          if (counter === promises.length) {
            resolve(arr);
          }
        }, r => {
          reject(r);
        })
      });
    });
  }

  //race
  static race(promises) {
    return new Promise((resolve, reject) => {
      promises.forEach(item => {
        //调用 then 方法
        item.then(v => {
          resolve(v);
        }, r => {
          reject(r);
        })
      })
    })
  }
}